<?php
declare (strict_types = 1);

namespace app\controller;

use app\middleware\Auth as AuthMiddleware;
use think\Request;

class Hostinfo
{
    protected $middleware = [AuthMiddleware::class];
    private $toast = 'public/toast';
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        $data=\app\model\Hostcat::hasWhere('hostinfo',['hostcat_id'=>$id])->with('hostinfo')->select();
//        dd($data->toArray());
        return view('index', [
            'list'  => $data->toArray(),
            'hostcat_id'=> $id,
        ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request,$id)
    {
        //
        $data = $request->param();
        return view('edit', [
            'obj'   =>  \app\model\Hostinfo::find($id),
            'hostcat_id'=> $data['hostcat_id'],
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->param();
        if (!$data['logo']){
            $data['logo'] = \app\model\Hostinfo::where('id',$data['id'])->column('logo');
        }
        return \app\model\Hostinfo::update($data) ? view($this->toast, [
            'infos'     =>  ['恭喜，修改成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/hostinfo/'.$data['hostcat_id'])
        ]) : '修改失败';
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
